if( SERVER ) then
	AddCSLuaFile( "shared.lua" );
end

if( CLIENT ) then
	SWEP.PrintName = "Knife";
	SWEP.Slot = 3;
	SWEP.SlotPos = 3;
	SWEP.DrawAmmo = false;
	SWEP.DrawCrosshair = false;
end

SWEP.Author			= "slay3r36"
SWEP.Instructions	= ""
SWEP.Contact		= ""
SWEP.Purpose		= ""

SWEP.Base 			= "gma_base"

SWEP.ViewModelFOV	= 62
SWEP.ViewModelFlip	= false

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.NextStrike = 0;
  
SWEP.ViewModel      = Model("models/weapons/v_knife_t.mdl")
SWEP.WorldModel   = Model("models/weapons/w_knife_t.mdl")
  

SWEP.Primary.Delay			= 0.9 
SWEP.Primary.Recoil			= 0
SWEP.Primary.Damage			= 15
SWEP.Primary.NumShots		= 1		
SWEP.Primary.Cone			= 0 
SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic   	= true
SWEP.Primary.Ammo         	= "none"
 

SWEP.Secondary.Delay		= 0.9
SWEP.Secondary.Recoil		= 0
SWEP.Secondary.Damage		= 0
SWEP.Secondary.NumShots		= 1
SWEP.Secondary.Cone			= 0
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic   	= true
SWEP.Secondary.Ammo         = "none"

util.PrecacheSound("weapons/knife/knife_deploy1.wav")
util.PrecacheSound("weapons/knife/knife_hitwall1.wav")
util.PrecacheSound("weapons/knife/knife_hit1.wav")
util.PrecacheSound("weapons/knife/knife_hit2.wav")
util.PrecacheSound("weapons/knife/knife_hit3.wav")
util.PrecacheSound("weapons/knife/knife_hit4.wav")
util.PrecacheSound("weapons/iceaxe/iceaxe_swing1.wav")

function SWEP:Initialize()
	if( SERVER ) then
		self:SendHoldType( "normal" );
	end
	self.Hit = { 
	Sound( "weapons/knife/knife_hitwall1.wav" )};
	self.FleshHit = {
  	Sound( "weapons/knife/knife_hit1.wav" ),
	Sound( "weapons/knife/knife_hit2.wav" ),
	Sound( "weapons/knife/knife_hit3.wav" ),
  	Sound( "weapons/knife/knife_hit4.wav" ) };

end

function SWEP:Deploy()
	self.Owner:EmitSound( "weapons/knife/knife_deploy1.wav" );

	return true;
end

function SWEP:PrimaryAttack()
	if( CurTime() < self.NextStrike ) or self.Owner:KeyDown( IN_ATTACK2 ) then return; end
	self.NextStrike = ( CurTime() + 0.5 );
	
	self.Owner:SetAnimation( PLAYER_ATTACK1 );
	self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );
	
	if SERVER then
		self:SendHoldType("melee")
	end
	
	local trace = self.Owner:GetEyeTrace();
	if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
		if( trace.Entity:IsPlayer() or trace.Entity:IsNPC() or trace.Entity:GetClass()=="prop_ragdoll" ) then

				if trace.Entity:IsPlayer() or trace.Entity:IsNPC() then
						local bullet = {}
						bullet.Src		= self.Owner:GetShootPos()
						bullet.Attacker = self.Owner
						bullet.Dir		= self.Owner:GetAimVector()
						bullet.Spread		= Vector(0.01,0.01,0)
						bullet.Num		= 1
						bullet.Damage		= 15
						bullet.Force		= 2
						/*bullet.Tracer		= 1	
						bullet.TracerName	= "Tracer"*/
						self:FireBullets(bullet)
				end
			
				
			self.Owner:EmitSound( self.FleshHit[math.random(1,#self.FleshHit)] );
		
		else
			
			self.Owner:EmitSound( self.Hit[math.random(1,#self.Hit)] );
		
		end
		local Pos1 = trace.HitPos + trace.HitNormal
		local Pos2 = trace.HitPos - trace.HitNormal
		local Bone = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone or 0 )
		if ( !Bone ) then
			Bone = trace.Entity
		end
		Pos1 = Bone:WorldToLocal( Pos1 )
		Pos2 = Bone:WorldToLocal( Pos2 )
		util.Decal( "manhackcut", Bone:LocalToWorld(Pos1), Bone:LocalToWorld(Pos2) )
	else
		self.Owner:SetAnimation( PLAYER_ATTACK1 );
		self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );
		self.Weapon:EmitSound("weapons/iceaxe/iceaxe_swing1.wav")
	end
end

function SWEP:checkRightClick(id)
	if !ValidEntity(self.Owner) then
		timer.Destroy("checkrightclick"..id)
		return
	end
	if !self.Owner:KeyDown( IN_ATTACK2 ) then
		self:SendHoldType("melee");
		self.Owner.scale = 1; 
		timer.Destroy("checkrightclick"..id)
		self.Owner:ChatPrint("timer destroy")
	end
	self.Owner:ChatPrint("Scale :" .. self.Owner.scale)
end

function SWEP:SecondaryAttack()
	if SERVER then
		if( CurTime() < self.NextStrike ) then return end
		self.NextStrike = ( CurTime() + 1 )
		
		self.Owner.scale = 0.1
		self:SendHoldType("melee2")
		local id = tostring(self.Owner:UniqueID()) or "unknown"
		if !timer.IsTimer("checkrightclick"..id) then
			timer.Create("checkrightclick"..id, 0.5, 0, self.checkRightClick, self, id) 
		end
		//timer.Simple(0.5, self.checkRightClick, self) 
	end
end 

if CLIENT then
	SWEP.ClRollAng = 0
end
function SWEP:GetViewModelPosition( pos, ang )
	if self.Owner:KeyDown( IN_ATTACK2 ) then
		if self.ClRollAng > -25 then
			self.ClRollAng = self.ClRollAng -1
		end
	else
		if self.ClRollAng < 0 then
			self.ClRollAng = self.ClRollAng +1
		end
	end
	return pos, Angle(ang.p, ang.y, self.ClRollAng)
end